package File;
/**
 * 字节/字符输入流read的几种方式
 */

import java.io.*;
import java.util.Scanner;

public class TestDemo2 {
    public static void main7(String[] args) throws IOException {
        //文本文件更简单的读写方法
        InputStream inputStream=new FileInputStream("./MyTestFolder/MyTestFile.txt");
        Scanner scanner=new Scanner(inputStream);
        System.out.println(scanner.next());
        inputStream.close();
    }
    public static void main6(String[] args) throws IOException {
        //通过Reader进行读写文本文件
        Reader reader=new FileReader("./MyTestFolder/MyTestFile.txt");
        char[] array=new char[1024];
        int len=reader.read(array);
        System.out.println("字符个数："+len);
        for(int i=0;i<len;i++){
            System.out.println(array[i]);
        }
    }
    public static void main5(String[] args) {
        //文本文件读取方法的优化
        //try with resources:在try执行技术后，会自动调用inputStream的close()方法
        //前提：只有实现了Closeable接口，才能这么干
        try(InputStream inputStream=new FileInputStream("./MyTestFolder/MyTestFile.txt");){
            byte[] b=new byte[1024];
            int len=inputStream.read(b);
            String s=new String(b,0,len,"utf8");
            System.out.println(s);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main4(String[] args) {
        //文本文件读写
        InputStream inputStream=null;
        try{
            inputStream=new FileInputStream("./MyTestFolder/MyTestFile.txt");
            byte[] b=new byte[1024];
            int len=inputStream.read(b);
            String s=new String(b,0,len,"utf8");
            System.out.println(s);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();//防止资源泄露
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main3(String[] args) throws IOException {
        //从下标off开始存放，之前的数据默认为0，长度为len
        InputStream inputStream=new FileInputStream("./MyTestFolder/MyTestFile.txt");
        byte[] array=new byte[1024];
        int len=inputStream.read(array,3,4);
        for (int i = 0; i < len; i++) {
            System.out.println(array[i]);
        }
        inputStream.close();
    }
    public static void main2(String[] args) throws IOException {
        //通过数组方法read
        InputStream inputStream=new FileInputStream("./MyTestFolder/MyTestFile.txt");
        byte[] array=new byte[1024];
        int len=inputStream.read(array);
        for (int i = 0; i < len; i++) {
            System.out.println(array[i]);
        }
        inputStream.close();
    }
    public static void main1(String[] args) throws IOException {
        //通过循环遍历读，遇到EOF就跳出循环
        InputStream inputStream=new FileInputStream("./MyTestFolder/MyTestFile.txt");
        while(true){
            int b=inputStream.read();
            if(b==-1) break;
            System.out.println(b);
        }
        inputStream.close();
    }
}
